// /////////////////////////////////////////////////////////////////////////////
//
// Name:            asScriptHelper.h
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Contains various convenience functions which can be used
//                  for binding to AngelScript.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __SCRIPTHELPER_H__
#define __SCRIPTHELPER_H__

#include "../config.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

//! Global function for assigning objects.
template<typename T>
T& assignT(const T &copy)
{
    T *t = new T(copy);
    return *t;
}

//! Global function for AngelScript's ref casting.
//! @author Andreas Jönsson (AngelScript's author)
template<typename A, typename B>
B* asRefCast(A* a)
{
    // If the handle already is a null handle, then just return the null handle
    if( !a ) return 0;

    // Now try to dynamically cast the pointer to the wanted type
    B* b = dynamic_cast<B*>(a);
    if( b == 0 )
    {
        // Since the cast couldn't be made, we need to release the handle we received
        a->drop();
    }
    return b;
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

#endif
